/*
 * [程序名称] Weather auto sender
 * [参考资料] http://code.google.com/p/auto-weather-sender/ 
 * [开源协议] Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.joe.weather;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

public class SettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
	private static final String SYSTEM_SETTING = "systemSwitch";
	private static final String SMS_OBSERVER_SETTING = "smsFuncSwitch";
	private static final String SEND_TIME_SETTING = "sendTime";
	private static final String NAME_SETTING = "callName";
	private static final String CITY_SETTING = "cityName";
	
	private CheckBoxPreference systemPreference;
	private CheckBoxPreference smsObserverPreference;
	private ListPreference sendTimePreference;
	private ListPreference namePreference;
	private ListPreference cityPreference;
	
	private static String[] sendTime_keyArray;
	private static String[] sendTime_valueArray;
	private static String[] name_keyArray;
	private static String[] name_valueArray;
	private static String[] city_keyArray;
	private static String[] city_valueArray;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.xml.setting);
		
		initUI();
		
		initResources();
	}

	private void initUI()
	{
		systemPreference = (CheckBoxPreference) getPreferenceScreen().findPreference(SYSTEM_SETTING);
		smsObserverPreference = (CheckBoxPreference) getPreferenceScreen().findPreference(SMS_OBSERVER_SETTING);
		
		sendTimePreference = (ListPreference) getPreferenceScreen().findPreference(SEND_TIME_SETTING);
		namePreference = (ListPreference) getPreferenceScreen().findPreference(NAME_SETTING);
		cityPreference = (ListPreference) getPreferenceScreen().findPreference(CITY_SETTING);
	}

	private void initResources()
	{
		// send time setting
		sendTime_keyArray = getResources().getStringArray(R.array.sendtime_key_list_preference);
		sendTime_valueArray = getResources().getStringArray(R.array.sendtime_value_list_preference);

		// name setting
		name_keyArray = getResources().getStringArray(R.array.name_key_list_preference);
		name_valueArray = getResources().getStringArray(R.array.name_value_list_preference);
		
		// city setting
		city_keyArray = getResources().getStringArray(R.array.city_key_list_preference);
		city_valueArray = getResources().getStringArray(R.array.city_value_list_preference);
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
	{
		if(key.equals(SYSTEM_SETTING))
		{
			
		}
		else if(key.equals(SMS_OBSERVER_SETTING))
		{
			
		}
		else if(key.equals(SEND_TIME_SETTING))
		{
			updateSummaryForSendtime();
		}
		else if(key.equals(NAME_SETTING))
		{
			updateSummaryForName();
		}
		else if(key.equals(CITY_SETTING))
		{
			updateSummaryForCity();
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		
		updateAllSummary();
		
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	private void updateAllSummary()
	{
		updateSummaryForSendtime();
		updateSummaryForName();
		updateSummaryForCity();
	}
	
	private void updateSummaryForSendtime()
	{
		String valueInArray = getPreferenceScreen().getSharedPreferences().getString(SEND_TIME_SETTING, "-1");
		if("-1".equals(valueInArray))
		{
			valueInArray = sendTime_valueArray[0];
			sendTimePreference.setDefaultValue(sendTime_valueArray[0]);
		}
		final String keyInArray = sendTime_keyArray[sendTimePreference.findIndexOfValue(valueInArray)];
		sendTimePreference.setSummary("发送时间是：" + keyInArray);
	}
	
	private void updateSummaryForName()
	{
		String valueInArray = getPreferenceScreen().getSharedPreferences().getString(NAME_SETTING, "-1");
		if("-1".equals(valueInArray))
		{
			valueInArray = name_valueArray[0];
			namePreference.setDefaultValue(name_valueArray[0]);
		}
		final String keyInArray = name_keyArray[namePreference.findIndexOfValue(valueInArray)];
		
		namePreference.setSummary("当前的称呼是：" + keyInArray);
	}
	
	private void updateSummaryForCity()
	{
		String valueInArray = getPreferenceScreen().getSharedPreferences().getString(CITY_SETTING, "-1");
		if("-1".equals(valueInArray))
		{
			valueInArray = city_valueArray[0];
			cityPreference.setDefaultValue(city_valueArray[0]);
//			cityPreference.setValueIndex(0);
		}
		final String keyInArray = city_keyArray[cityPreference.findIndexOfValue(valueInArray)];
		cityPreference.setSummary("你选择的城市是：" + keyInArray);
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}
	
}
